﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using Infragistics.Win.UltraMessageBox;

namespace KD.Sms
{
    public partial class frmBase : Form
    {
        public frmBase()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmBase_Load(object sender, EventArgs e)
        {
            try
            {
                timerShow.Start();
                toolSysManager.Visible = CacheUser.E_IsAdmin;
                lblUser.Text = CacheUser.Chinese_name;
                if (CacheMemoryCenter.E_PackingMachineState == null)
                    CacheMemoryCenter.E_PackingMachineState = new Entity.PackingMachineState();
                CacheMemoryCenter.M_CurrentMsgBox = risMsg;
                CacheSamplingPacking.DoInit();
                //读取全局配置参数
                CacheMemoryCenter.E_IsDebug = ConfigurationManager.AppSettings["IsDebug"].ToString() == "1";
                CacheMemoryCenter.E_DeviceName = ConfigurationManager.AppSettings["DeviceName"].ToString();
                CacheMemoryCenter.E_NoSampling = ConfigurationManager.AppSettings["NoSampling"].ToString() == "1";
                CacheMemoryCenter.E_Unmanned = ConfigurationManager.AppSettings["Unmanned"].ToString() == "1";

                string _cyjIp = ConfigurationManager.AppSettings["Cyj_Ip"].ToString();
                int _cyjCycle = int.Parse(ConfigurationManager.AppSettings["Cyj_Cyle"].ToString());
                int _cyjBegin = int.Parse(ConfigurationManager.AppSettings["Cyj_Begin"].ToString());
                int _cyjLength = int.Parse(ConfigurationManager.AppSettings["Cyj_Length"].ToString());
                string _fzjIp = ConfigurationManager.AppSettings["Fzj_Ip"].ToString();
                int _fzjCycle = int.Parse(ConfigurationManager.AppSettings["Fzj_Cyle"].ToString());
                int _fzjBegin = int.Parse(ConfigurationManager.AppSettings["Fzj_Begin"].ToString());
                int _fzjLength = int.Parse(ConfigurationManager.AppSettings["Fzj_Length"].ToString());

                CacheSystemControl.E_ConnectWl = Convert.ToBoolean(int.Parse(ConfigurationManager.AppSettings["ConnectWl"].ToString()));
                CacheSystemControl.E_ConnectSjbm = Convert.ToBoolean(int.Parse(ConfigurationManager.AppSettings["ConnectSjbm"].ToString()));
                CacheSystemControl.E_AlreadyJudgeWeight = Convert.ToDecimal(ConfigurationManager.AppSettings["AlreadyJudgeWeight"].ToString());
                Task.Cyj.Instance.DoStart("采样机", _cyjIp, _cyjCycle, _cyjBegin, _cyjLength);
                Task.Fzj.Instance.DoStart("封装机", _fzjIp, _fzjCycle, _fzjBegin, _fzjLength);

                UI_Operate.frmSamplingCar objCar = new UI_Operate.frmSamplingCar();
                Monitor.frmElecMotor objMotor = new Monitor.frmElecMotor();
                Monitor.frmProtect objProtect = new Monitor.frmProtect();
                Monitor.frmRunFault objFault = new Monitor.frmRunFault();
                Monitor.frmRunState objState = new Monitor.frmRunState();
                PlateRecognition.frmLpr objLpr = new PlateRecognition.frmLpr();
                //北侧
                Monitor.frmDepartOne objDepartOne = new Monitor.frmDepartOne();
                //南侧
                Monitor.frmDepartOne objDepartTwo = new Monitor.frmDepartOne();
                Monitor.frmPail objPail = new Monitor.frmPail();
                DoOpenMenu(objCar, panel2);
                DoOpenMenu(objMotor, panel6);
                DoOpenMenu(objProtect, panel9);
                DoOpenMenu(objFault, panel5);
                DoOpenMenu(objState, panel4);
                DoOpenMenu(objLpr, panel3);
                if (CacheMemoryCenter.E_DeviceName == "北侧采样机")
                    DoOpenMenu(objDepartOne, panel8);
                else
                    DoOpenMenu(objDepartTwo, panel8);
                DoOpenMenu(objPail, panel10);

                //删除15天前日志
                Log.LogHelper.DoClearSrart(15);
                ActionCenter.Action.Instance.ShowSystemWaring += OnShowSystemWaring;
            }
            catch (Exception ex)
            {
                UI.MsgBox.Show(UI.MsgType.Error, "程序主窗体加载出现未知异常，详见日志！");
                Log.LogHelper.DoWriteLogTypeSubitem(Log.LogType.Error, Log.LogSubitem.System, "程序主窗体加载出现未知异常，详见日志！");
                Log.LogHelper.DoWriteLogTypeSubitem(Log.LogType.Error, Log.LogSubitem.System, ex.ToString());
            }

        }

        /// <summary>
        /// 系统告警
        /// </summary>
        /// <param name="send"></param>
        /// <param name="e"></param>
        UltraMessageBoxInfo info = new UltraMessageBoxInfo();
        private void OnShowSystemWaring(object send, ActionCenter.WaringEventArgs e)
        {
            info.Header = "系统告警：请仔细阅读当前提示信息";
            info.Caption = "系统告警";
            info.Buttons = MessageBoxButtons.OK;
            info.Text = string.Empty;
            foreach (string str in e.E_List)
            {
                info.Text += str + "\r\n";
            }
            Msg.ShowMessageBox(info);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="frm"></param>
        /// <param name="title"></param>
        private void DoOpenMenu(Form frm, Panel pane)
        {
            frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            frm.TopLevel = false;
            frm.Dock = DockStyle.Fill;
            pane.Controls.Add(frm);
            frm.WindowState = FormWindowState.Maximized;//如果windowState设置为最大化，添加到tabPage中时，winform不会显示出来
            frm.Show();
        }

        private void timerShow_Tick(object sender, EventArgs e)
        {
            lblTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>
        /// 采样信息查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 采样信息SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UI_Query.frmSamplingInfo obj = new UI_Query.frmSamplingInfo();
            obj.Show();
        }

        private void 样桶信息PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UI_Query.frmSamplingInfoSimple obj = new UI_Query.frmSamplingInfoSimple();
            obj.Show();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 故障查询EToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UI_Query.frmFault obj = new UI_Query.frmFault();
            obj.Show();
        }

        bool _bCloseing = false;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmBase_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!_bCloseing)
            {
                if (MessageBox.Show("是否确定退出系统?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    DAL.SqlLite.Main.Instance.LogWrite(SysLogType.Login, "成功退出系统");
                    _bCloseing = true;
                    Card.MfMain.Instance.DoStopRead();
                    Application.Exit();
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        private void 网络状态NToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UI_Query.frmNet obj = new UI_Query.frmNet();
            obj.Show();
        }

        private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UI_Sys.frmChangePwd obj = new UI_Sys.frmChangePwd();
            obj.ShowDialog();
        }

        private void 用户管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UI_Sys.frmUser obj = new UI_Sys.frmUser();
            obj.ShowDialog();
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UI_Sys.frmAbout obj = new UI_Sys.frmAbout();
            obj.ShowDialog();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 数据打包ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UI_Operate.frmOutPut obj = new UI_Operate.frmOutPut();
            obj.ShowDialog();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 弃料完成ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("请确定当前已检查过弃料仓，不存在堵料的情况！", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                DAL.SqlLite.Main.Instance.LogWrite(SysLogType.Login, "成功点击弃料完成菜单");

                if (DAL.SqlLite.Main.Instance.DoUpdateWasteComplete())
                    MessageBox.Show("操作成功");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 阈值设定ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UI_Manager.frmParms obj = new UI_Manager.frmParms();
            obj.ShowDialog();
        }

        private void 布点分布率BToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UI_Query.frmSamplingPosInfo obj = new UI_Query.frmSamplingPosInfo();
            obj.Show();
        }

        private void 强制采样wToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UI_Operate.frmUploadWl obj = new UI_Operate.frmUploadWl();
            obj.Show();
        }

        private void 用户日志LToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UI_Sys.frmSysLog obj = new UI_Sys.frmSysLog();
            obj.Show();
        }

        private void 测试TToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmDebug obj = new frmDebug();
            obj.ShowDialog();
        }

        private void 运行参数RToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UI_Manager.frmSysParms obj = new UI_Manager.frmSysParms();
            obj.ShowDialog();
        }

        private void 日志查看LToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UI_Sys.frmLog obj = new UI_Sys.frmLog();
            obj.Show();
        }

        private void 车型模拟数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UI_Sys.frmUnattended obj = new UI_Sys.frmUnattended();
            obj.ShowDialog();
        }
    }
}
